using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;

namespace Omniture.API
{
	public class TrackReferenceCollection : List<TrackReference>, IXmlable
	{
		#region Class Members

		private PageItem _parent = null;
		private ITrackItem _trackItem = null;
		private TrackItemTypeConstants _collectionType = 0;

		#endregion

		#region Constructors

		internal TrackReferenceCollection(PageItem parent, TrackItemTypeConstants collectionType)
    {
			_parent = parent;
			_collectionType = collectionType;
		}

		#endregion

		#region Property Implementations

		public PageItem Parent
    {
			get { return _parent; }
		}

		public ITrackItem TrackItem
		{
			get { return _trackItem; }
		}

		#endregion

		#region Methods

		public TrackReference NewItem(ITrackItem trackItem)
		{
			this.Parent.Parent.LastObjectIndex++;
			TrackReference retval = new TrackReference(this.Parent, trackItem, this.Parent.Parent.LastObjectIndex);
			this.Add(retval);
			return retval;
		}

		public void Clean(ITrackItem item)
		{
			for (int ii = this.Count - 1; ii >= 0; ii--)
			{
				ITrackItem o = this[ii].TrackItem;
				if (o.Id == item.Id)
					RemoveAt(ii);
			}
		}

		public TrackReference FindByObject(ITrackItem item)
		{
			foreach (TrackReference t in this)
			{
				if (t.TrackItem.Id == item.Id)
					return t;
			}
			return null;
		}

		public TrackReference this[string name]
    {
      get
      {
        foreach(TrackReference c in this)
        {
					if (string.Compare(c.TrackItem.Name, name, true) == 0)
            return c;
        }
        return null;
      }
    }

    public TrackReference GetById(int id)
    {
      foreach(TrackReference c in this)
      {
        if(c.Id == id)
          return c;
      }
      return null;    
		}

    public bool Contains(string name)
    {
      foreach(TrackReference t in this)
      {
				if (string.Compare(t.TrackItem.Name, name, true) == 0)        
          return true;
      }
      return false;
    }

		public bool Contains(int index)
		{
			return (0 <= index) && (index < this.Count);
		}

		public bool ContainsId(int columnId)
		{
			foreach (TrackReference t in this)
			{
				if (t.Id == columnId)
					return true;
			}
			return false;
		}

		public int IndexOf(string name)
		{

			for (int ii = 0; ii < this.Count; ii++)
			{
				if (this[ii].TrackItem.Name == name)
					return ii;
			}
			return -1;
		}

		public void Remove(string name)
		{
			TrackReference removeItem = null;
			foreach (TrackReference t in this)
			{
				if (string.Compare(t.TrackItem.Name, name, true) == 0)
					removeItem = t;
			}

			if (removeItem != null)
				this.Remove(removeItem);
		}

		public int GetNextUniqueId()
		{
			int retval = 1;
			foreach (TrackReference item in this)
			{
				if (retval <= item.Id)
					retval = item.Id + 1;
			}
			return retval;
		}

		#endregion
		
		#region IXmlable Members

		public virtual void LoadXml(XmlNode parentNode)
		{
			_collectionType = (TrackItemTypeConstants)Enum.Parse(typeof(TrackItemTypeConstants), XmlHelper.GetAttribute(parentNode, "CollectionType", _collectionType.ToString("d")));
			foreach (XmlNode node in parentNode.ChildNodes)
			{
				TrackReference newItem = new TrackReference(this.Parent);
				newItem.LoadXml(node);
				if (newItem.TrackItem != null) this.Add(newItem);
			}
		}

		public virtual void SaveXml(XmlNode parentNode)
		{
			//Now loop and add children
			foreach (TrackReference t in this)
			{
				if (_parent.Parent.GetTrackObject(t.TrackItem.Id) != null)
				{
					XmlNode newNode = XmlHelper.AddNode(parentNode, "Reference");
					t.SaveXml(newNode);
				}
			}
		}

    #endregion

	}
}
